home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 04 / winstdio / enumwnd.c < prev    next >
C/C++ Source or Header  |  1991-07-01  |  4KB  |  141 lines

  1. /* 
  2. ENUMWND.C
  3.  
  4. Microsoft C, Windows SDK:
  5. cl -c -AS -Gsw -Oais -Zpe enuwnd.c winio.c wmhandlr.c
  6. link enumwnd winio wmhandlr,enumwnd,nul,/nod slibcew libw,winio.def
  7. rc winio.rc enumwnd.exe
  8.  
  9. Borland C++:
  10. bcc -WS -G -O -Z -w-par -Hu enumwnd.c winio.c wmhandlr.c
  11. copy winio.rc enumwnd.rc
  12. rc winio.rc enumwnd.exe
  13. */
  14.  
  15. #include "windows.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "winio.h"
  19. #include "wmhandlr.h"
  20.  
  21. #define BUF_LEN     128
  22.  
  23. FARPROC enum_w = 0;
  24. int num_wnd = 0;
  25.  
  26. void display_window(HWND hWnd, int level)
  27. {
  28.     RECT r;
  29.     char *buf;
  30.     int i;
  31.     for (i=0; i<(int)level; i++)
  32.         printf("   ");
  33.     printf("%04X   ", hWnd);
  34.     if (! (buf = malloc(BUF_LEN))) 
  35.         return;
  36.     if (GetClassName(hWnd, buf, BUF_LEN))
  37.         printf("[%s]   ", buf);
  38.     GetWindowRect(hWnd, &r);
  39.     printf("(%d,%d,%d,%d)   ", r.left, r.top, r.right, r.bottom);
  40.     if (GetWindowText(hWnd, buf, BUF_LEN))
  41.         printf("<%s>", buf);
  42.     putchar('\n');
  43.     if (level == 0)
  44.     {
  45.         HANDLE hInst = GetWindowWord(hWnd, GWW_HINSTANCE);
  46.         if (GetModuleFileName(hInst, buf, BUF_LEN))
  47.             printf("   %s\n", buf);
  48.     }
  49.     free(buf);
  50. }
  51.     
  52. /* MSC _export keyword: use instead of EXPORTS in .DEF file */
  53. BOOL _export FAR PASCAL EnumW(HWND hWnd, DWORD level)
  54. {
  55.     static HWND seen[1024] = {0};
  56.     HWND *ps;
  57.     int i;
  58.     
  59.     /* cheap check for duplicates */
  60.     for (i=0, ps=seen; i<num_wnd; i++, ps++)
  61.         if (hWnd == *ps)
  62.             return 1;
  63.     if (num_wnd < 1024)
  64.         seen[num_wnd++] = hWnd;
  65.  
  66.     display_window(hWnd, level);
  67.     EnumChildWindows(hWnd, enum_w, level+1);
  68.     return 1;   /* keep going */
  69. }
  70.  
  71. void enumerate_windows(void)
  72. {
  73.     num_wnd = 0;
  74.     winio_clear();
  75.     winio_setpaint(FALSE);
  76.     display_window(GetDesktopWindow(), 0);
  77.     if (! EnumWindows(enum_w, 0))
  78.         puts("EnumWindows failed");
  79.     winio_setpaint(TRUE);
  80. }
  81.     
  82. #define MENU_REFRESH    100
  83. #define MENU_ABOUT      101
  84.  
  85. void exit_func(void)
  86. {
  87.     if (enum_w) FreeProcInstance(enum_w);
  88. }
  89.  
  90. WMHANDLER prev_command;
  91.  
  92. long on_command(HWND hwnd, unsigned message, WORD wParam, LONG lParam)
  93. {
  94.     // can see where switch comes in: small now, tendency to grow though!
  95.     switch (wParam)
  96.     {
  97.         case MENU_REFRESH:
  98.             enumerate_windows();
  99.             return 0;
  100.         case MENU_ABOUT:
  101.             MessageBox(hwnd, "Enumerate Windows\n"
  102.                              "Version 1.0\n"
  103.                              "(c) Andrew Schulman, April 1991\n"
  104.                              "WINIO (c) David Maxey, April 1991",
  105.                 "Enumerate Windows", MB_OK);
  106.             return 0;
  107.         default:
  108.             return (*prev_command)(hwnd, message, wParam, lParam);
  109.     }
  110. }
  111.  
  112. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, 
  113.     LPSTR lpCmdLine, int nCmdShow)
  114. {
  115.     HWND hwnd;
  116.     HMENU hmenu, hpopup;
  117.  
  118.     winio_settitle("Enumerate Windows");
  119.     if (! winio_init(hInstance, hPrevInstance, nCmdShow, 0))
  120.         return 1;
  121.     
  122.     prev_command = wmhandler_set(WM_COMMAND, on_command);
  123.     
  124.     hpopup = CreatePopupMenu();
  125.     AppendMenu(hpopup, MF_STRING, MENU_REFRESH, "&Refresh");
  126.     AppendMenu(hpopup, MF_STRING, MENU_ABOUT, "&About...");
  127.     hmenu = CreateMenu();
  128.     AppendMenu(hmenu, MF_POPUP, hpopup, "&EnumWnd");
  129.     hwnd = winio_hwnd();
  130.     SetMenu(hwnd, hmenu);
  131.     DrawMenuBar(hwnd);
  132.     
  133.     winio_setfont(ANSI_FIXED_FONT);
  134.     atexit(exit_func);
  135.     enum_w = MakeProcInstance((FARPROC) EnumW, hInstance);
  136.     enumerate_windows();
  137.     winio_end();
  138.     return 0;
  139. }
  140.  
  141.